package serializable.kryo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class KryoDemo {
	@Test
	public void toFile() {
		Kryo kryo = new Kryo();
		A a = new A();
		a.setA("a1");
		try {
			kryo.register(A.class);
			Output output;
			output = new Output(new FileOutputStream("a.kryo"));
			kryo.writeObject(output, a);
			output.flush();
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
	@Test
	public void toObject() {
		Kryo kryo = new Kryo();
		try {
			kryo.register(A.class);
			Input input = new Input(new FileInputStream("a.kryo"));
			A a = kryo.readObject(input, A.class);
			System.out.println(JSONObject.toJSONString(a));
			input.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
}
